//
//  NBTabBarViewController.m
//  v2ex-shelves
//
//  Created by Mac os on 2018/4/8.
//  Copyright © 2018年 Mac os. All rights reserved.
//

#import "NBTabBarViewController.h"

@interface NBTabBarViewController ()

@end

@implementation NBTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray * normalImamges =  @[@"tabBar_inquiry",@"tabBar_record",@"tabBar_diary",@"tabBar_mine"];
    // 选中图片
    NSArray * selectedImages = @[@"tabBar_inquiry_highlight",@"tabBar_record_highlight",@"tabBar_diary_highlight",@"tabBar_mine_highlight"];
    
    
    NSArray * arr = @[@"NBHomepageViewController",@"NBLookForViewController",@"NBDiscoverViewController", @"NBMineViewController"];
    
    
    // 文字
    NSArray *titles = @[NSLocalizedString(@"首页", @"home"),NSLocalizedString(@"节点", @"Nodes"),NSLocalizedString(@"搜索", @"search"),NSLocalizedString(@"我的", @"MyMessage")];
    
    [self creatTabbarVCWithControllersArray:arr andTabbarNormalImageArr:normalImamges andTabbarSelectedImageArr:selectedImages andTabbarsTitlesArr:titles];
    // 设置默认选中的视图
    self.selectedIndex = 0;
    // 设置默认选中的tabbar按钮
    
    ((UIButton *)[self.tabBarView.subviews objectAtIndex:0]).selected= YES;
}



- (void)creatTabbarVCWithControllersArray:(NSArray *)vcArray andTabbarNormalImageArr:(NSArray *)normalArr andTabbarSelectedImageArr:(NSArray *)TabbarSelectedArr andTabbarsTitlesArr:(NSArray *)titlesArr
{
    // 创建tabbarItems
    self.tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, tabbarHeight+SafeAreaInsetsBottom)];
    self.tabBarView.layer.shadowColor =UIColorFromRGB(0x434343).CGColor;
    self.tabBarView.layer.shadowOffset =CGSizeMake(0, -3);
    self.tabBarView.layer.shadowOpacity = 0.1;
    self.tabBarView.layer.shadowRadius = 1;
    self.tabBarView.backgroundColor = [UIColor whiteColor];
    self.tabBarView.userInteractionEnabled = YES;
    self.tabBar.backgroundColor = [UIColor whiteColor];
    [self.tabBar addSubview:self.tabBarView];
    NSMutableArray * array = [[NSMutableArray alloc] init];
    for (int i=0; i<vcArray.count; i++)
    {
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(i*ScreenWidth/vcArray.count, 0, ScreenWidth/vcArray.count, tabbarHeight)];
        // UIViewContentModeScaleAspectFill也会证图片比例不变，但是是填充整个ImageView的
        btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        btn.tag = i+100;
        [btn addTarget:self action:@selector(tabBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:titlesArr[i] forState:UIControlStateNormal];
        [btn setTitleColor:UIColorFromRGB(0x919191) forState:UIControlStateNormal];
        [btn setTitleColor:UIColorFromRGB(0x3cbfae) forState:UIControlStateSelected];
        [btn setImage:[UIImage imageNamed:normalArr[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:TabbarSelectedArr[i]] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont fontWithName:FONT_REGULAR size:12];
        [self.tabBarView addSubview:btn];
        [btn setImagePosition:LXMImagePositionTop spacing:0];
        UIViewController * vc = [[NSClassFromString(vcArray[i]) alloc] init];
        NBbaseNavigationController * cvc = [[NBbaseNavigationController alloc] initWithRootViewController:vc];
        [array addObject:cvc];
        
    }
    self.viewControllers = array;
}


- (void)tabBarBtnClick:(UIButton *)sender
{
    
    for (int i =0;i<self.viewControllers.count;i++)
    {
        UIButton * button = (UIButton *)[self.tabBarView viewWithTag:i+100];
        button.selected = NO;
    }
    sender.selected = YES;
    NSInteger indexx = sender.tag;
    self.selectedIndex = indexx;
}

-(void)selectBar:(NSUInteger )sele
{
    
    self.selectedIndex = sele;
    // 设置默认选中的tabbar按钮
    for (int i=0; i<4; i++) {
        if (i==sele) {
            ((UIButton *)[self.tabBarView.subviews objectAtIndex:i+100]).selected = YES;
        }else
        {
            ((UIButton *)[self.tabBarView.subviews objectAtIndex:i+100]).selected = NO;
        }
    }
    
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
   
    NSUInteger tabIndex = [tabBar.items indexOfObject:item];

    if (tabIndex != self.selectedIndex)
    {
        UIButton *btn = (UIButton*)[self.tabBarView viewWithTag: tabIndex+100];
        [self tabBarBtnClick:btn];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
